package com.nondo.dean.product.service;

import com.nondo.dean.product.domain.ProductLog;
import com.nondo.dean.product.domain.ProductLogExample;
import com.nondo.dean.product.domain.ProductWithBLOBs;
import com.nondo.dean.product.mapper.ProductLogMapper;
import com.nondo.dean.product.mapper.ProductMapper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/3/28
 */
@Component
public class ProductLogService {
    public static final String TYPE_ADD = "0";//添加
    public static final String TYPE_DELETE = "1";//删除
    public static final String TYPE_EDIT = "2";//编辑

    @Autowired
    UserService userService;
    
    @Autowired
    ProductLogMapper productLogMapper;

    @Autowired
    ProductMapper productMapper;

    /**
     * 添加
     *
     * @param productLog
     * @return
     */
    public int add(ProductLog productLog) {
        return this.productLogMapper.insertSelective(productLog);
    }

    /**
     * 查询日志列表
     *
     * @param productId
     * @return
     */
    public List<ProductLog> selectByProductId(int productId) {
        ProductLogExample productLogExample = new ProductLogExample();
        productLogExample.createCriteria().andProductIdEqualTo(productId);

        return this.productLogMapper.selectByExample(productLogExample);
    }

    /**
     * 获取列表
     *
     * @param offset
     * @param limit
     * @param productLog
     * @return
     */
    public List<ProductLog> getList(int offset, int limit, ProductLog productLog) {
        ProductLogExample example = new ProductLogExample();
        example.setOrderByClause("create_date DESC");
        RowBounds rowBounds = new RowBounds(offset, limit);
        ProductLogExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productLog);
        return productLogMapper.selectByExampleWithRowbounds(example, rowBounds);
    }

    /**
     * 获取列表
     *
     * @param productLog
     * @return
     */
    public List<ProductLog> getList(ProductLog productLog) {
        ProductLogExample example = new ProductLogExample();
        example.setOrderByClause("create_date DESC");
        ProductLogExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productLog);
        return productLogMapper.selectByExample(example);
    }

    /**
     * 数据统计
     *
     * @param productLog
     * @return
     */
    public Long count(ProductLog productLog) {
        ProductLogExample example = new ProductLogExample();
        ProductLogExample.Criteria criteria = example.createCriteria();
        setCriteria(criteria, productLog);
        return productLogMapper.countByExample(example);
    }

    /**
     * 设置查询条件
     *
     * @param criteria
     * @param productLog
     */
    void setCriteria(ProductLogExample.Criteria criteria, ProductLog productLog) {
        if (productLog != null) {
            if (productLog.getId() != null) {
                criteria.andIdEqualTo(productLog.getId());
            }
        }
        criteria.andDelFlagEqualTo(ProductLog.DEL_FLAG_NORMAL);
    }

    /**
     * 处理产品变更日志
     *
     * @param productWithBLOBsOrig
     * @param productWithBLOBs
     */
    public void handleLog(ProductWithBLOBs productWithBLOBsOrig, ProductWithBLOBs productWithBLOBs) {
        //1、判断数据是否为新增还是更新
        if (org.springframework.util.StringUtils.isEmpty(productWithBLOBsOrig)) {
            //新增
        } else {
            String message = null;
            //更新
            if (productWithBLOBs.getShelfStatus() != null && !productWithBLOBsOrig.getShelfStatus().equals(productWithBLOBs.getShelfStatus())) {
                //上下架操作
                message = (productWithBLOBs.getShelfStatus() == ProductService.SHELF_STATUS_DOWN) ? "进行了上架操作" : "进行了下架操作";

            } else if (productWithBLOBs.getGuidePrice() != null && !productWithBLOBsOrig.getGuidePrice().equals(productWithBLOBs.getGuidePrice())) {
                //价格变更
                message = "产品指导价从" + productWithBLOBsOrig.getGuidePrice() + "修改为" + productWithBLOBs.getGuidePrice();

            } else {
                //其他操作
                message = "编辑了商品";
            }
            ProductLog productLog = new ProductLog();
            productLog.setType(TYPE_EDIT);
            productLog.setProductId(productWithBLOBs.getId());
            productLog.setBody(message);
            productLog.setUserId(userService.getLoginUserId());
            productLog.setUserName(userService.getLoginUserName());

            this.productLogMapper.insertSelective(productLog);
        }
    }
}
